import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {GoodsService} from "@goods/goods.service";
import {Subject} from "rxjs";
import {Router} from "@angular/router";
import {ToolsService} from '@shared/service/tools/tools.service';
import {ImageServeService} from "@shared/service/image/image-serve.service";

@Component({
  selector: 'goods-nav',
  templateUrl: './goods-nav.component.html',
  styleUrls: ['./goods-nav.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class GoodsNavComponent implements OnInit {
  public dataStore = {
    goodsType: []
  };
  private ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(private goodsService: GoodsService,
              private router: Router,
              private tools: ToolsService,
              private  imageService: ImageServeService) {
  }

  /**
   * @description 获取商品类型
   * @author ouyqx-691
   * @date 2017-11-16
   */
  getGoodsType() {
    this.tools.showLoading();
    this.goodsService.getGoodsType().takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.tools.hideLoading();
      if (res.result === '0' && res.data && res.data.goodsCategoryList) {
        this.dataStore.goodsType = res.data.goodsCategoryList;
      }
    });
  };


  /**
   * @description go to goods list page
   * @param nav :nav object
   */
  goToNavTarget(nav) {
    if (nav.targetUrl) {  //  点击全部
      this.router.navigate(['/goods/nav']);
      return;
    }
    if (!nav.serviceUrl) { return; }; // 未配置链接

    if (nav.navType === '0') { // 商品导航
      const goodsCategoryCode = nav.serviceUrl.split('goodsCategoryCode=')[1];
      if (goodsCategoryCode) {
        this.router.navigate(['/goods/list/', goodsCategoryCode], {
          queryParams: {
            title: encodeURI(nav.serviceName)
          }
        });
      }
      // goodsCategoryCode && this.router.navigate(['/goods/list/', goodsCategoryCode], {
      //   queryParams: {
      //     title: encodeURI(nav.serviceName)
      //   }
      // });
    } else { // 专题导航
      const topicId = nav.serviceUrl.split('topicId=')[1];
      if (topicId) {
        this.router.navigate(['/goods/topic/', topicId]);
      }
    }
  }

  ngOnInit() {
    this.getGoodsType();
  }
}
